package com.itheima.reggie.common;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//@WebFilter("/*")
@Slf4j
public class LoginCheckFilter implements Filter {
    private static final AntPathMatcher ANT_PATH_MATCHER=new AntPathMatcher();
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    /**
     * 拦截未登录的商家
     * @param request
     * @param response
     * @param chain
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        //判断商家是否登录
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;

        /*//0. 禁止远程访问
        String remoteAddr = request.getRemoteAddr();
        System.out.println(remoteAddr);
        if (!remoteAddr.equals("0:0:0:0:0:0:0:1")) {
            return;
        }*/

        //拦截到的路径
        String requestURI = req.getRequestURI();
        log.info("拦截到的路径{}",requestURI);
        String[] urls = {
                "/employee/login",
                "/employee/logout",
                "/backend/**",
                "/front/**",
                "/user/sendMsg",
                "/user/login"
        };
        //放行的路径
        if (chock(urls, requestURI)) {
            log.info("不需要处理的路径{}",requestURI);
            chain.doFilter(request, response);
            return;
        }
        //需要处理的用户判断是否登录
        if(req.getSession().getAttribute("employee")!=null){
            //登录放行的路径
            log.info("登录放行的路径{}",requestURI);

            //把用户的id放到threalocal线程域中
            Long empId = (Long) req.getSession().getAttribute("employee");
            BaseContext.setCurrentId(empId);

            chain.doFilter(request, response);
            return;
        }

        //移动端需要处理的用户判断是否登录
        if(req.getSession().getAttribute("user")!=null){
            //登录放行的路径
            log.info("登录放行的路径{}",requestURI);

            //把用户的手机号放到threalocal线程域中
            Long userId = (Long) req.getSession().getAttribute("user");
            BaseContext.setCurrentId(userId);

            chain.doFilter(request, response);
            return;
        }


        //未登录的路径
        log.info("未登录的路径{}",requestURI);
        resp.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));

    }

    /**
     * 检查地址是否放行
     * @param urls
     * @param requestURI
     * @return
     */
    public boolean chock(String[] urls,String requestURI){
        for (String url : urls) {
            boolean match = ANT_PATH_MATCHER.match(url, requestURI);
            if(match){
                return true;
            }
        }
        return false;
    }


}
